package com.nxy.cpfsfe.util;

import org.apache.commons.codec.binary.Base64;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 通用工具类
 */
public class CommonUtil {




	public static long caculateDateDiff(String start, String end) throws ParseException {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
		Date startDate = simpleDateFormat.parse(start);
		Date endDate = simpleDateFormat.parse(end);
		return  startDate.getTime() - endDate.getTime();
	}
	
	public static String getDate(){
		return getDate("yyyyMMdd");
	}
	
	public static String getDate(String format) {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		String sndDt = sdf.format(date);
		return sndDt;
	}

	public static String getTimeStamp(){
		return getTime("yyyyMMddHHmmssSS");
	}

	public static String getTime() {
		return getTime("HHmmss");
	}
	
	public static String getTime(String format) {
		Date date = new Date();
		DateFormat df = new SimpleDateFormat(format);
		String sndTm = df.format(date);
		return sndTm;
	}


	/**
	 * 根据包含的字符串过滤队列
	 * @param map
	 * @param containChars
	 * @return
	 */
	public static Map<String,String> filterMap(Map<String,String> map, String containChars){
		return map.entrySet().stream()
				.filter(e -> e.getKey().contains(containChars))
				.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
	}


	/**
	 * 根据包含的字符串过滤队列,并将key转为list
	 * @param map
	 * @param containChars
	 * @return
	 */
	public static List<String> filterMapValueToList(Map<String,String> map, String containChars){
		return  map.entrySet().stream()
				.filter(e -> e.getKey().contains(containChars))
				.map(e -> e.getValue()).collect(Collectors.toList());
	}

	public static byte[] encodeBase64(byte[] buf) {
		if (buf == null)
			return null;
		return Base64.encodeBase64(buf);
	}

	
}
